package com.elitetyc.customview.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.elitetyc.customview.R;

/**
 * Created by elitetyc on 2019/5/21.
 * Describe:
 */
public class ScrollerShowItem extends ViewGroup {

    private View centerView;
    private View righltView;

    private Scroller scroller;


    public ScrollerShowItem(Context context) {
        this(context,null);
    }

    public ScrollerShowItem(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScrollerShowItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scroller = new Scroller(context);
//        加载布局进入view
//        LayoutInflater.from(context).inflate(R.layout.scroller_show_item,this);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        设置中间布局的宽高是当前线性布局的宽高，因为默认只显示中间布局
        centerView = this.getChildAt(0);
        centerView.measure(widthMeasureSpec, heightMeasureSpec);
//        设置右边布局的宽度是右边布局的，高度是线性布局的高度
        righltView = this.getChildAt(1);
        righltView.measure(righltView.getLayoutParams().width,heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
//        设置中间布局显示
        centerView.layout(l, t, r, b);
//        设置右边布局显示在屏幕的右边
        righltView.layout(r,0,r+righltView.getLayoutParams().width,b);
    }

    private int mLastX;

    private boolean mIsMain = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        return super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
//                记录手指按下的位置
                mLastX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
//                记录移动过程中的x值
                int x = (int) event.getX();
                if (x-mLastX<0){
//                    说明是左滑，就开始展示右边的删除按钮
                    this.scrollBy(mLastX-x,0);
                }else {
//                    说明是右滑动
                    this.scrollBy(mLastX-x,0);
                }
                mLastX = x;
                break;
            case MotionEvent.ACTION_UP:
//                获取滑动了多少
                int scrollX = getScrollX();
//                如果是左滑动，并且滑动的距离大于删除按钮的一半，就显示整个按钮
                if (scrollX>0&&scrollX>righltView.getLayoutParams().width/2){
//                    这时就是需要显示删除按钮
//                    scrollTo(righltView.getLayoutParams().width,0);
                    mIsMain = false;
                }else {
//                    显示主页面
//                    scrollTo(0,0);
                    mIsMain = true;

                }
                switchMenu();
                break;

        }
        return true;
    }

    private void switchMenu() {
        mIsMain = !mIsMain;
        switchScreen();
    }

    private void switchScreen() {
        int startX = getScrollX();
        int deltax;
        if (!mIsMain){
            deltax = 0-startX;
        }else {
            deltax = righltView.getLayoutParams().width - startX;
        }
        scroller.startScroll(startX,0,deltax,0,Math.abs(deltax)*3);
        invalidate();


    }

    @Override
    public void computeScroll() {
//        super.computeScroll();
        if (scroller.computeScrollOffset()){
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            postInvalidate();
        }

    }
}
